package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smartfactory.sail.entity.Cust;
import com.smartfactory.sail.entity.enums.CustCooperationStatusEnum;
import com.smartfactory.sail.entity.enums.CustTypeEnum;
import com.smartfactory.sail.mapper.CustMapper;
import com.smartfactory.sail.service.ICustService;
import com.smartfactory.sail.utils.CodeUtils;
import com.smartfactory.sail.utils.DateUtils;
import com.smartfactory.sail.utils.PageUtils;
import com.smartfactory.sail.vo.PageParam;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Service
public class CustServiceImpl extends ServiceImpl<CustMapper, Cust> implements ICustService {

    private CustMapper custMapper;

    public CustServiceImpl(CustMapper custMapper) {
        this.custMapper = custMapper;
    }

    @Override
    public IPage<Cust> listSuppliers(Map<String, Object> queryParam, PageParam pageParam) {
        //分页信息
        Page<Cust> page = new Page<>();
        PageUtils.createPage(page, pageParam);
        QueryWrapper<Cust> query = new QueryWrapper<>();
        queryParam.put("custType", 2);
        query.allEq(queryParam);
        return custMapper.selectPage(page, query);
    }

    @Override
    public IPage<Cust> listCust(Map<String, Object> queryParam, PageParam pageParam) {
        //分页信息
        Page<Cust> page = new Page<>();
        PageUtils.createPage(page, pageParam);
        QueryWrapper<Cust> query = new QueryWrapper<>();
        queryParam.put("custType", 1);
        query.allEq(queryParam);
        return custMapper.selectPage(page, query);
    }

    @Override
    public boolean updateCooperationTime(Long custId, String time) {
        Cust cust = new Cust();
        cust.setId(custId);
        cust.setRecentCooperationTime(time);
        return updateById(cust);
    }

    @Override
    public Long saveCustByOrderInfo(Cust cust, String contractType, String contractINfo) {
        cust.setCustCode(CodeUtils.creatCustCode());
        cust.setCustType(CustTypeEnum.DDKH);
        //首次合作时间
        cust.setFirstCooperationTime(DateUtils.getNowDate());
        cust.setRecentCooperationTime(DateUtils.getNowDate());
        cust.setCooperationDuration("1天");
        cust.setCooperationStatus(CustCooperationStatusEnum.HZZ);
        List<Map<String, Object>> maps = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("type", contractType);
        map.put("info", contractINfo);
        maps.add(map);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String contacts = objectMapper.writeValueAsString(maps);
            cust.setContactsInfo(contacts);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        save(cust);


        return cust.getId();
    }

    @Override
    public Cust queryCustByName(String custName) {
        QueryWrapper<Cust> query = new QueryWrapper<>();
        query.eq("enterpriseName", custName);
        return getOne(query);
    }
}
